// page/person/favor/fav.js
// page/person/exam/exam.js
var config = require('../../config');
var common = require('../utils/common.js');
var loadFav = require('loadFav.js');
var app = getApp();
var username = '';//用户登录id
var tab_page = {};
var page = 0;
var size = 7;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    lastLoadTime: 0,
    windowHeight: 0,
    clickId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    tab_page={};
    app.getUserName(function (user_name) {
      username = user_name;
      var data = {
        page: page,
        size: size
      }
      loadFav.load_page(data, tab_page, username, that);
    });
    wx.getSystemInfo({
      success: function (res) {
        //赋值
        that.setData({
          windowHeight: res.windowHeight - 30
        });
      }
    });  
  },
  loadMore: function () {
    var curPage = tab_page.page + 1;
    if (curPage <= tab_page.pageSize - 1) {
      var curTime = new Date().getTime();
      //上一次加载的时间
      var lastTime = this.data.lastLoadTime;
      that.setData({
        lastLoadTime: curTime
      });
      if (curTime - lastTime < 300) {
        return;
      }
      wx.showLoading({
        title: '加载中···',
      });
      var data = {
        page: curPage,
        size: size
      }
      app.getUserName(function (username) {
        loadFav.load_page(data, tab_page, username, that);
      });
      var timeout = setTimeout(function () {
        wx.hideLoading();
        clearTimeout(timeout);
      }, 500);
    }
  },refresh:function(){

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面

    prevPage.setData({
      collectionId: 0
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: config.shareDes,
      path: '/pages/index/index',
      imageUrl: '../images/sharepic_1.jpg'
    }
    // return {
    //   title: config.shareName,
    //   desc: config.shareDes,
    //   path: '/pages/collection/collection'
    // }
  },
  inplay_material_p: function (e) {//进入播放页面 父页面
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    if(id==null||id==0){
      return;
    }
    if (this.data.clickId==id){
        return;
    }
    this.setData({
      clickId:id
    });
    wx.navigateTo({
      url: '../video/video?id=' + id + "&title=" + title
    });
  },
  favor:function(e){//取消收藏
    var that=this;
    var id = e.currentTarget.dataset.id;
    if(id==0||id==null){
      return;
    }
    app.getUserName(function (user_name) {
      common.add_rem_favorite(id, username, 'remove',
        function(){
          var list= tab_page.list;
          for(var i=0;i<list.length;i++){
            if(id==list[i].id){
              list.splice(list[i],1);
              break;
            }
          }
          tab_page.list=list;
          that.setData({
            list:list
          });
        }
      );
    });
  },
  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.list.forEach(function (v, i) {
      if (v.isTouchMove)//只操作为true的
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      list: this.data.list
    })
  },
  //滑动事件处理
  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index,//当前索引
      startX = that.data.startX,//开始X坐标
      startY = that.data.startY,//开始Y坐标
      touchMoveX = e.changedTouches[0].clientX,//滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY,//滑动变化坐标
      //获取滑动角度
      angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
     that.data.list.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      list: that.data.list
    })
  },
  /**
   * 计算滑动角度
   * @param {Object} start 起点坐标
   * @param {Object} end 终点坐标
   */
  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },//删除单个收藏数据
   del:function(e){
     var id = e.currentTarget.dataset.id;
     var that = this;
     wx.showModal({
       title: '提示',
       content: '您确定要删除该条记录?',
       success: function (res) {
         if (res.confirm) {
           app.getUserName(function(username){
             if(username){
               wx.request({
                 url: config.materials + '/favor',
                 data: {
                   materialIds: id,
                   action: 'remove',
                   username: username
                 },
                 success: function (res) { }
               });
             }
             var list=that.data.list;
             var arrs=new Array();
             for(var i=0;i<list.length;i++){
               if(list[i].id!=id){
                 arrs.push(list[i]);
               }
             }
             tab_page.list = arrs;
             that.setData({
               list: arrs
             });
           });
         }
       }
     });         
  }


})